#pragma once

#include "AsyncLibDef.h"

namespace AsyncLib
{

class CTpTimerTask;

class CTpTimerManager
{
public:
	CTpTimerManager();
	~CTpTimerManager();

	void Init();
	void Stop();
	void Exit();
	std::shared_ptr<ITimerTask> CreateTimer(std::weak_ptr<CTpTimerManager> pManager);
	void SetTimer(PTP_TIMER pTpTimer, UINT nElapse);
	void DeleteTimer(PTP_TIMER pTpTimer);

private:
	void OnCallback(PTP_TIMER pTpTimer);

	std::shared_mutex m_mutex;
	std::unordered_map<PTP_TIMER, std::shared_ptr<CTpTimerTask>> m_mapTasks;
};

class CTpTimerTask : public ITimerTask
{
public:
	CTpTimerTask(std::weak_ptr<CTpTimerManager> pManager, PTP_TIMER pTpTimer) : m_pManager(std::move(pManager)), m_pTpTimer(pTpTimer)
	{
	}

	void Start(UINT nElapse) override;
	void Stop() override;
	void Close() override;

private:
	std::weak_ptr<CTpTimerManager> m_pManager;
	PTP_TIMER m_pTpTimer;
};

} // namespace AsyncLib
